#include "TabviewPinAssignment.h"
#include <memory>
#include "VAction.h"




struct TabviewPinAssignment_ActionParam{
  void* v;
  int Index;
};




TabviewPinAssignment::TabviewPinAssignment(QWidget *parent):QTableView(parent){

  setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);

  connect(this,&TabviewPinAssignment::customContextMenuRequested,this,&TabviewPinAssignment::SlotContextMenu);

  Model=new QStandardItemModel(this);

  Pins=nullptr;

  this->Row=0;
  this->Column=0;


  setModel(Model);
}

TabviewPinAssignment::~TabviewPinAssignment(){

  if(this->Pins!=nullptr){
    delete[] this->Pins;
  }

}




void TabviewPinAssignment::SlotContextMenu(QPoint pos){

  if(Model==nullptr){
    return;
  }

  QModelIndex index=indexAt(pos);

  if(index.isValid()==false){
    return;
  }

  Pin& pin=Pins[Column*index.row()+index.column()];
  const QStringList& stringList=pin.FunctionNames();
  qsizetype count=stringList.count();

  if(count<=0){
    return;
  }
  
  
  QMenu menu(this);

  VAction* Actions=new VAction[count];
  TabviewPinAssignment_ActionParam* ActionParams=new TabviewPinAssignment_ActionParam[count];
  
  for(int i=0;i<count;i++){

    ActionParams[i].v=pin.V();
    ActionParams[i].Index=i;

    Actions[i].setV(&ActionParams[i]);

    if(i==pin.Index()){
      Actions[i].setText(stringList[i]);
      Actions[i].setIcon(icon);
    }else{
      Actions[i].setText(stringList[i]);
    }

    connect(&Actions[i],static_cast<void (VAction::*)(void*,bool)>(&VAction::triggered),this,static_cast<void (TabviewPinAssignment::*)(void*,bool)>(&TabviewPinAssignment::triggered));

    menu.addAction(&Actions[i]);
  }

  menu.exec(QCursor::pos());//显示右键菜单
  menu.clear();
  delete[] ActionParams;
  delete[] Actions;

}

void TabviewPinAssignment::triggered(void* v,bool checked){
  TabviewPinAssignment_ActionParam* ActionParam;

  ActionParam=(TabviewPinAssignment_ActionParam*)v;

  emit triggered(ActionParam->v,ActionParam->Index,checked);

}




void TabviewPinAssignment::SetSize(int Row,int Column){

  Model->clear();

  Model->setRowCount(Row);
  Model->setColumnCount(Column);
  
  for(int i=0;i<Row;i++){
    for(int j=0;j<Column;j++){
      QStandardItem* item=new QStandardItem();
      item->setSelectable(false);
      Model->setItem(i,j,item);
    }
  }

  this->Row=Row;
  this->Column=Column;

}

void TabviewPinAssignment::SetData(QList<Pin*>& Pins){

  if(this->Pins!=nullptr){
    delete[] this->Pins;
  }

  this->Pins=new Pin[Row*Column];


  for(int i=0;i<Row;i++){
    for(int j=0;j<Column;j++){

      Pin& this_pin=this->Pins[Column*i+j];
      QStandardItem& item=Model->item(i,j)[0];

      this_pin.SetParam(Pins[Column*i+j][0]);

      int index;

      index=this_pin.Index();

      if(index<0){
        item.setText(this_pin.Name());
      }else{
        item.setText(this_pin.Name()+"\r\n"+this_pin.FunctionNames()[index]);
      }

      item.setBackground(this_pin.BgColor());
      item.setTextAlignment(Qt::AlignmentFlag::AlignCenter);

    }
  }

}

void TabviewPinAssignment::SetData(const Pin& pin,int Row,int Column){

  if(Pins==nullptr){
    return;
  }
  if((Row>=this->Row)||(Column>=this->Column)){
    return;
  }

  Pin& this_pin=Pins[this->Column*Row+Column];
  QStandardItem& item=Model->item(Row,Column)[0];

  this_pin.SetParam(pin);

  int index;

  index=this_pin.Index();

  if(index<0){
    item.setText(this_pin.Name());
  }else{
    item.setText(this_pin.Name()+"\r\n"+this_pin.FunctionNames()[index]);
  }

  item.setBackground(this_pin.BgColor());

}



